
package com.jf.cloud.search.listener;

import cn.throwx.canal.gule.model.CanalBinLogResult;
import com.jf.cloud.api.order.bo.EsOrderInvoiceBO;
import com.jf.cloud.api.order.feign.OrderFeignClient;
import com.jf.cloud.api.order.feign.OrderInvoiceFeignClient;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.order.bo.EsOrderBO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.search.bo.OrderBO;
import com.jf.cloud.search.constant.EsIndexEnum;
import com.jf.cloud.search.util.EsSearchUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author zz
 * @date 2020/11/13
 */
@Component
public class OrderCanalListener extends BaseCanalBinlogEventThrowExceptionProcessor<OrderBO> {

    private static final Logger log = LoggerFactory.getLogger(OrderCanalListener.class);

    @Autowired
    private OrderFeignClient orderFeignClient;
    @Autowired
    private OrderInvoiceFeignClient orderInvoiceFeignClient;

    /**
     * 插入订单，此时插入es
     */
    @Override
    protected void processInsertInternal(CanalBinLogResult<OrderBO> result) {
        Long orderId = result.getPrimaryKey();
        EsOrderBO esOrder = getEsOrderData(orderId);
        if (Objects.isNull(esOrder)) {
            log.error("订单信息异常的订单id为：{}", orderId);
            return;
        }
        EsSearchUtil.esSave(esOrder.getOrderId(),EsIndexEnum.ORDER, esOrder);
    }

    /**
     * 更新订单数据（同时更新订单对应发票的es数据）
     */
    @Override
    protected void processUpdateInternal(CanalBinLogResult<OrderBO> result) {
        OrderBO afterData = result.getAfterData();
        EsOrderBO esOrder = getEsOrderData(afterData.getOrderId());
        if (Objects.isNull(esOrder)) {
            log.error("订单信息异常的订单id为：{}", afterData.getOrderId());
            return;
        }
        EsSearchUtil.esUpdate(esOrder.getOrderId(),EsIndexEnum.ORDER, esOrder);
        // 更新发票的es数据
        EsOrderInvoiceBO esOrderInvoiceBO = this.getEsOrderInvoiceData(afterData.getOrderId());
        if (esOrderInvoiceBO != null) {
            EsSearchUtil.esUpdate(esOrder.getOrderInvoiceId(),EsIndexEnum.ORDER_INVOICE, esOrderInvoiceBO);
        }
    }

    private EsOrderBO getEsOrderData(Long orderId) {
        ServerResponseEntity<EsOrderBO> orderResponseEntity = orderFeignClient.saveMongoAndGetEsInfo(orderId);
        if (orderResponseEntity.isFail()) {
            throw new LuckException(orderResponseEntity.getMsg());
        }
        // 添加es中保存的订单数据
        return orderResponseEntity.getData();
    }

    /**
     * 获取订单发票的es数据
     * @param orderId
     * @return
     */
    private EsOrderInvoiceBO getEsOrderInvoiceData(Long orderId) {
        ServerResponseEntity<EsOrderInvoiceBO> response = orderInvoiceFeignClient.getEsOrderInvoiceByOrderId(orderId);
        if (response.isFail()) {
            throw new LuckException(response.getMsg());
        }
        return response.getData();
    }
}
